package com.rt;

import net.sf.json.JSONObject;
import org.cert.open.CertException;
import org.cert.open.CertToolV1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Demo class
 *
 * @author Administrator
 * @date 2019/4/1
 */
@Controller
@RequestMapping(value="/cncert")
public class CncertController {
    private static CertToolV1 tool = new CertToolV1();

    @RequestMapping(value="/idCardHash")
    @ResponseBody
    public Map<String, Object> idCardHash(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String idCard = request.getParameter("idCard");
        System.out.println("_________________________sdfsffweafa________________________");
        Map content  = new HashMap();
        content.put("idCardHash",tool.idCardHash(idCard));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/nameHash")
    @ResponseBody
    public Map<String, Object> nameHash(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String name = request.getParameter("name");
        Map content  = new HashMap();
        content.put("nameHash",tool.nameHash(name));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/phoneHash")
    @ResponseBody
    public Map<String, Object> phoneHash(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String phone = request.getParameter("phone");
        Map content  = new HashMap();
        content.put("phoneHash",tool.phoneHash(phone));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/batchNumber")
    @ResponseBody
    public Map<String, Object> batchNumber(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String sourceCode = request.getParameter("sourceCode");
        String tradeDate = request.getParameter("tradeDate");
        String num = request.getParameter("num");
        String seqId = request.getParameter("seqId");
        Map content  = new HashMap();

        content.put("batchNumber",tool.batchNumber(sourceCode, tradeDate, num, seqId));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/checkCode")
    @ResponseBody
    public Map<String, Object> checkCode(HttpServletRequest request) throws CertException, UnsupportedEncodingException {
        Map map = new HashMap();
        String timestamp = request.getParameter("timestamp");
        Map content  = new HashMap();
        content.put("checkCode",tool.checkCode(timestamp));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getApiKey")
    @ResponseBody
    public Map<String, Object> getApiKey(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String apiKey = request.getParameter("apiKey");
        String sourceCode = request.getParameter("sourceCode");
        String versionStr = request.getParameter("versionStr");
        Long currentTime = Long.parseLong(request.getParameter("currentTime"));
        String nonce = request.getParameter("nonce");
        Map content  = new HashMap();
        content.put("apiKey",tool.getApiKey(apiKey,sourceCode,versionStr,currentTime,nonce));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getIdcardAscription")
    @ResponseBody
    public Map<String, Object> getIdcardAscription(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String idcard = request.getParameter("idcard");
        Map content  = new HashMap();
        content.put("idcardAscription",tool.getIdcardAscription(idcard));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getSex")
    @ResponseBody
    public Map<String, Object> getSex(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String idcard = request.getParameter("idcard");
        Map content  = new HashMap();
        content.put("sex",tool.getSex(idcard));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getAge")
    @ResponseBody
    public Map<String, Object> getAge(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String idcard = request.getParameter("idcard");
        Map content  = new HashMap();
        content.put("age",tool.getAge(idcard));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getPhoneAscription")
    @ResponseBody
    public Map<String, Object> getPhoneAscription(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String phonenum = request.getParameter("phonenum");
        Map content  = new HashMap();
        content.put("phoneAscription",tool.getPhoneAscription(phonenum));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

    @RequestMapping(value="/getCompanyAscription")
    @ResponseBody
    public Map<String, Object> getCompanyAscription(HttpServletRequest request) throws CertException {
        Map map = new HashMap();
        String registrationNum = request.getParameter("registrationNumber");
        Map content  = new HashMap();
        content.put("companyAscription",tool.getCompanyAscription(registrationNum));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }


    /**
     * 组合请求(用户接口)
     * @param idcard
     * @param phonenum
     * @param registrationNum
     * @return
     * @throws CertException
     *
     * */
    @RequestMapping(value="/getUserInfo")
    @ResponseBody
    public Map<String, Object> getUserInfo(@RequestParam(value="idcard") String idcard,@RequestParam(value="phonenum") String phonenum,@RequestParam(value="registrationNum") String registrationNum) throws CertException {
        Map map = new HashMap();

        Map content  = new HashMap();
        if(!(null==registrationNum||"".equals(registrationNum))){
            content.put("userProvince", tool.getCompanyAscription(registrationNum));
        }
        content.put("phoneAscription", tool.getPhoneAscription(phonenum));
        JSONObject json = tool.phoneHash(phonenum);
        String phoneHash = json.getString("phone");
        String salt = json.getString("salt");
        content.put("phoneHash", phoneHash);
        content.put("userUuid", salt);
        content.put("userIdcardHash", tool.idCardHash(idcard));
        content.put("userAscription", tool.getIdcardAscription(idcard));
        content.put("userAge", tool.getAge(idcard));
        content.put("userSex", tool.getSex(idcard));

        map.put("result", 0);
        map.put("data",content);
        return map;
    }

}